﻿using System;
using System.Collections;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using CzfSDK;

namespace Server
{
	public static class ArenaManager
	{
		const int MAX_WIN_PRIZE = 200; // 每天最多多少次获胜奖励
		
		const int MIN_STARS = 19; // 保底多少星级

		static int[] minStars = new int[]{ 
			60, 90, 120, 150
		}; // 阶段保底

		// 等级多少会跳级
		static HashSet<int> lvAdd = new HashSet<int>(new int[]{
			3,7,11,15,19,23,27,31,35,39,43,47,51,55,59,65,71,77,83,89,95,101,107,113,119,125,131,137,143,149,150
		});

		// 初始化跳级规则
		public static void CheckLvAdd()
		{
			lvAdd.Clear ();
			var temps = MConfig.GetArenaTemplate ();
			foreach (var temp in temps.Values) {
				int id = temp.GetVal<int> ("id");
				int stars = temp.GetVal<int> ("star");

				if (stars == 0 && id > 0) {
					lvAdd.Add (id - 1);
				}
			}
		}

		// 读取玩家的竞技场信息
		public static Hashtable GetUserArena(int userId, int category)
		{
			var ret = Get (userId, category);
			if (ret == null) {
				Add (userId, category);
				ret = Get (userId, category);
			}
			return ret;
		}

		// 玩家竞技场战斗结算
		public static Hashtable FightEnd(int userId, int category, bool win)
		{
			var ret = new Hashtable ();
			var arena = GetUserArena (userId, category);
			var id = arena.GetVal<int> ("id");
			var winCount = arena.GetVal<int> ("win");
			var stars = arena.GetVal<int> ("stars");

			ret ["oldStars"] = stars;  // 结算前的胜点
			ret ["oldWin"] = winCount;

			if (win) {
				// 处理胜点
				if (winCount >= 2) {
					// 连胜
					stars = AddLv (stars, 2);
				} else {
					stars = AddLv (stars, 1);
				}

				// 处理连胜纪录
				if (winCount < 0)
					winCount = 1;
				else
					winCount += 1;
			} else {
				// 计算保底
				var minStar = MIN_STARS;
				foreach (int m in minStars) {
					if (stars >= m && m > minStar)
						minStar = m;
				}

				if (stars > minStar) { // 保底不掉星
					// 处理胜点
					stars = AddLv (stars, -1);
				}

				// 处理连胜
				if (winCount > 0)
					winCount = -1;
				else
					winCount -= 1;
			}

			Update (id, winCount, stars);

			ret ["newStars"] = stars;  // 结算后的胜点
			ret ["newWin"] = winCount;

			int todayWin;
			var todaywintime = arena.GetVal<DateTime>("todaywintime");
			if (todaywintime.Year == DateTime.Now.Year && todaywintime.DayOfYear == DateTime.Now.DayOfYear) {
				// 是当天的记录
				todayWin = arena.GetVal<int> ("todaywin");
			} else {
				// 不是当天的记录
				todayWin = 0;
			}

			string prize = "";

			if (win) {
				if (todayWin < MAX_WIN_PRIZE) {
					// 更新奖励次数
					UpdateWinCount(id, todayWin + 1);
					prize = "1,50|2,10";
					// 获胜奖励10金币
					BagManager.AddItem(prize, userId, "竞技场获胜奖励");
				}
			}

			ret ["maxWinCount"] = MAX_WIN_PRIZE; // 每天最多获得多少次奖励
			ret ["winCount"] = todayWin; // 当天已经获得多少次奖励
			ret ["prize"] = prize; // 奖励,空字符串表示没有奖励

			return ret;
		}

		static int AddLv(int old, int add)
		{
			if (add < 1) {
				return old + add;
			} else if (add > 1) {
				return AddLv ((AddLv(old, 1)), add - 1);
			} else {
				if (lvAdd.Contains (old))
					return old + 2;
				else
					return old + 1;
			}
		}


		#region DB
		// 更新玩家的竞技场信息
		private static int UpdateWinCount(int id, int winPrizeCount)
		{
			var sql = "update arena set todaywin=@todaywin,todaywintime=@todaywintime where id=@id";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", id),
				new MySqlParameter ("@todaywin", winPrizeCount),
				new MySqlParameter ("@todaywintime", DateTime.Now)
			);
		}

		// 更新玩家的竞技场信息
		private static int Update(int id, int win, int stars)
		{
			var sql = "update arena set stars=@stars,win=@win where id=@id";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", id),
				new MySqlParameter ("@stars", stars),
				new MySqlParameter ("@win", win)
			);
		}

		// 新增玩家的竞技场信息
		private static int Add(int userId, int category)
		{
			var sql = "insert into arena(userid, category, createtime, updatetime) values(@userid, @category, @createtime, @updatetime)";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@category", category),
				new MySqlParameter ("@createtime", DateTime.Now),
				new MySqlParameter ("@updatetime", DateTime.Now)
			);
		}


		// 获取指定玩家的竞技场信息
		public static Hashtable Get(int userId, int category)
		{
			var sql = "select * from arena where userid = @userid and category = @category";
			var dr = MysqlPool.current.ExecDataRow (sql,
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@category", category)
			);
			if (dr == null)
				return null;
			else
				return dr.ToHashtable ();
		}
		#endregion
	}
}

